/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mtetris;
import java.awt.*;

public class gamewin extends Canvas{
    private blocks[][] tBlocks = new blocks[7][4];
    private int[][] map = new int[10][20];
    private Color[][] mapColor = new Color[10][20];
    private int currentBlock, currentStyle;
    private Image offScreenImage;
    public gamewin(){
        this.gameInit();
//        gBuff_2.fill3DRect(120, 32, 10, 10, true);
    }
    public void gameInit(){
        this.initTBlock();
        this.setBounds(0, 0, 400, 600);
        this.setBackground(Color.BLACK);
        getCurrent().setPos(145, 120);
        currentBlock = 0;
        currentStyle = 0;
        for(int i=0;i<10;i++)
            for(int j=0;j<20;j++){
                map[i][j]=0;
                mapColor[i][j]=new Color(0);
            }
    }
    private void initArea (Graphics g){
        g.setColor(Color.DARK_GRAY);
        //g.fillRect(36, 88, 214, 435);
        g.fillRect(45, 120, 200, 400);
    }
    private void initTBlock (){
        for(int i=0;i<7;i++)
            for(int j=0;j<4;j++)
                tBlocks[i][j] = new blocks();

        tBlocks[0][0].setBlocks(-1,0,1,0,0,1,Color.RED);
        tBlocks[0][1].setBlocks(0,-1,-1,0,0,1,Color.RED);
        tBlocks[0][2].setBlocks(0,-1,1,0,-1,0,Color.RED);
        tBlocks[0][3].setBlocks(0,-1,1,0,0,1,Color.RED);
        
        tBlocks[1][0].setBlocks(-1,0,1,0,2,0,Color.BLUE);
        tBlocks[1][1].setBlocks(0,-1,0,1,0,2,Color.BLUE);
        tBlocks[1][2].setBlocks(-1,0,1,0,2,0,Color.BLUE);
        tBlocks[1][3].setBlocks(0,-1,0,1,0,2,Color.BLUE);
        
        tBlocks[2][0].setBlocks(1,-1,1,0,0,1,Color.YELLOW);
        tBlocks[2][1].setBlocks(-1,0,1,1,0,1,Color.YELLOW);
        tBlocks[2][2].setBlocks(1,-1,1,0,0,1,Color.YELLOW);
        tBlocks[2][3].setBlocks(-1,0,1,1,0,1,Color.YELLOW);
        
        tBlocks[3][0].setBlocks(1,0,1,1,0,1,Color.MAGENTA);
        tBlocks[3][1].setBlocks(1,0,1,1,0,1,Color.MAGENTA);
        tBlocks[3][2].setBlocks(1,0,1,1,0,1,Color.MAGENTA);
        tBlocks[3][3].setBlocks(1,0,1,1,0,1,Color.MAGENTA);
        
        tBlocks[4][0].setBlocks(0,-1,0,1,1,1,Color.CYAN);
        tBlocks[4][1].setBlocks(1,0,-1,0,-1,1,Color.CYAN);
        tBlocks[4][2].setBlocks(-1,-1,0,-1,0,1,Color.CYAN);
        tBlocks[4][3].setBlocks(1,-1,1,0,-1,0,Color.CYAN);

        tBlocks[5][0].setBlocks(0,-1,0,1,-1,1,Color.ORANGE);
        tBlocks[5][1].setBlocks(-1,0,1,0,-1,-1,Color.ORANGE);
        tBlocks[5][2].setBlocks(0,-1,0,1,1,-1,Color.ORANGE);
        tBlocks[5][3].setBlocks(1,0,-1,0,1,1,Color.ORANGE);

        tBlocks[6][0].setBlocks(0,1,-1,-1,-1,0,Color.WHITE);
        tBlocks[6][1].setBlocks(0,-1,1,-1,-1,0,Color.WHITE);
        tBlocks[6][2].setBlocks(0,1,-1,-1,-1,0,Color.WHITE);
        tBlocks[6][3].setBlocks(0,-1,1,-1,-1,0,Color.WHITE);
    }
    public blocks getCurrent(){
        return tBlocks[currentBlock][currentStyle];
    }
    public int getCurrentBlock(){
        return currentBlock;
    }
    public int getCurrentStyle(){
        return currentStyle;
    }
    public void setCurrent(int block, int style){
        currentBlock = block;
        currentStyle = style;
    }
    public void doneCurrentBlock(){
        this.getCurrent().setBlock(map, mapColor);
    }
    public int leftAble(){
        if(this.getCurrent().testLeft(map)==0)
            return 0;
        return 1;
    }
    public int rightAble(){
        if(this.getCurrent().testRight(map)==0)
            return 0;
        return 1;
    }
    public int downAble(){
        if(this.getCurrent().testDown(map)==0)
            return 0;
        return 1;
    }
    public int changeAble(){
       blocks tmpl = new blocks();
       blocks tmpr = new blocks();
       tmpl = tBlocks[this.getCurrentBlock()][(this.getCurrentStyle()+1)%4];
       tmpr = tBlocks[this.getCurrentBlock()][(this.getCurrentStyle()+1)%4];
       tmpl.setPos(this.getCurrent().getX(), this.getCurrent().getY());
       tmpr.setPos(this.getCurrent().getX(), this.getCurrent().getY());
       if(this.getCurrent().testDown(map)==0||
          tmpr.testRight(map)==0||
          tmpl.testLeft(map)==0)
            return 0;
        return 1;
    }
    private void paintMap (Graphics g){
        for(int i=0;i<10;i++){
            for(int j=0;j<20;j++){
                if(map[i][j]==1){
                    g.setColor(mapColor[i][j]);
                    g.fillRect(i*20+45, j*20+120, 19, 19);
                    this.repaint();
                }
            }
        }
    }
    @Override
    public void update(Graphics g){
        if(this.offScreenImage == null) {
            this.offScreenImage = this.createImage(400, 600);
        }
        //2.得到缓冲图像的画笔
        Graphics gOffScreen = this.offScreenImage.getGraphics();
        //3.绘制缓冲图像
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.black);
        gOffScreen.fillRect(0, 0, 400, 600);
        gOffScreen.setColor(c);
        //4.调用paint(),将缓冲图象的画笔传入
        paint(gOffScreen);
        //5.再将此缓冲图像一次性绘到代表屏幕的Graphics对象，即该方法传入的“g”上
        g.drawImage(offScreenImage, 0, 0, null);
    }
    @Override
    public void paint(Graphics g){
        this.initArea(g);
        getCurrent().paintBlock(g);
        this.paintMap(g);
        int flag;
        for(int i=0;i<20;i++){
            flag=1;
            for(int j=0;j<10;j++){
                if(map[j][i]==0){
                    flag=0;
                    break;
                    }
                }
            if(flag==1){
                    for(int k=0;k<10;k++)
                    {
                        map[k][i]=0;
                        map[k][0]=0;
                    }
                    for(int a=i;a>0;a--)
                        for(int b=0;b<10;b++){
                            map[b][a]=map[b][a-1];
                            mapColor[b][a]=mapColor[b][a-1];
                        }
                    Toolkit Ttoolkit;
                    Ttoolkit = Toolkit.getDefaultToolkit();
                    Ttoolkit.beep();
                    
                }
            }
    }
}
